package cn.wolfcode.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiredPermission(name = "员工显示", expression = "employee:list")
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("departments", departments);
        return "/employee/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }

    @RequestMapping("/batchDelete")
    @RequiredPermission(name = "员工批量删除", expression = "employee:batchDelete")
    public String batchDelete(Long[] ids) {
        if (ids != null && ids.length > 0) {
            employeeService.batchDelete(ids);
        }
        return "redirect:/employee/list";
    }

    @RequestMapping("/input")
    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    public String input(Long id, Model model) {
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        model.addAttribute("roles", roleService.listAll());
        if (id != null) {
            Employee employee = employeeService.listOne(id);
            model.addAttribute("employee", employee);
        }
        return "/employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    public String saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() != null) {
            employeeService.update(employee, ids);
        } else {
            employeeService.insert(employee, ids);
        }
        return "redirect:/employee/list";
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String username) {
        Employee employee = employeeService.selectByName(username);
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid", employee == null);
        return map;
    }

    @RequestMapping("/exportXls")
    @RequiredPermission(name = "员工导出", expression = "employee:exportXls")
    public void exportXls(HttpServletResponse response, String keyword, Long deptId) throws IOException {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = employeeService.exportXls(keyword, deptId);
        //将excel文件输出到响应率流中
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    @RequiredPermission(name = "员工导入", expression = "employee:importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
           return employeeService.importXls(file);

    }
    @RequestMapping("/forbin")
    @RequiredPermission(name = "员工权限开通/禁止", expression = "employee:forbin")
    public String forbin(Long id)  {
        if (id != null) {
            employeeService.updateEmpStatus(id);
        }
        return "redirect:/employee/list";
    }
}
